package com.shujia.hw;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class RandomUser {
    FileReader fileReader;
    BufferedReader bufferedReader;

    FileReader namefileReader;

    BufferedReader nameBufferedReader;

    ArrayList<String> nameArrayList;

    ArrayList<String> surnameList;


    File surName;

    File name;

    User user;

    public RandomUser(File surName, File name) throws IOException {
//         new FileReader("redisCode/data/姓氏.txt");
        try {
            fileReader = new FileReader(surName);
            bufferedReader = new BufferedReader(fileReader);
            namefileReader = new FileReader(name);
            nameBufferedReader = new BufferedReader(namefileReader);

            // 初始化过程需要完成对数据的读取
            nameArrayList = CreateUser.getNameArrayList(nameBufferedReader);
            surnameList = CreateUser.getSurnameList(bufferedReader);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        user = new User();
    }

    public User createOneUser(int id) throws IOException {


        // 建表语句
        // create table user(id int, name varchar(200),age int, accumulate int, userClazz int,modify_time timestamp default current_timestamp);


        String name = CreateUser.getName(surnameList, nameArrayList);
        int age = CreateUser.getAge(16, 30);
        int accumulate = CreateUser.getAccumulate(100, 10000);
        int userClazz = CreateUser.getUserClazz(1, 6);
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        user.setAccumulate(accumulate);
        user.setUserClazz(userClazz);
        System.out.println(user);
        return user;
    }

    public void close() {
        try {
            bufferedReader.close();
            nameBufferedReader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}

class CreateUser {


    private static Random random = new Random();

    private CreateUser() {
    }

    public static ArrayList<String> getSurnameList(BufferedReader bufferedReader) throws IOException {
        String line = "";

        ArrayList<String> surnameList = new ArrayList<>();

        while ((line = bufferedReader.readLine()) != null) {
            //
            for (String word : line.replace(" ", "").replace("\"", "").split(",")) {
                surnameList.add(word);
            }
        }

        return surnameList;
    }

    public static ArrayList<String> getNameArrayList(BufferedReader nameBufferedReader) throws IOException {


        // 2.名字随机汉字
        String line = "";

        ArrayList<String> nameList = new ArrayList<>();

        while ((line = nameBufferedReader.readLine()) != null) {
            //
            for (String word : line.replace("\"", "").split("、")) {
                nameList.add(word);
            }
        }

        return nameList;
    }

    public static String getName(ArrayList<String> surnameList, ArrayList<String> nameList) throws IOException {


//        System.out.println(surnameList);
//        System.out.println(nameList);


        int i = random.nextInt(surnameList.size());
        String surname = surnameList.get(i);

        int nameNum = random.nextInt(2) + 1;
        String name = "";
        for (int j = 0; j < nameNum; j++) {
            name += nameList.get(random.nextInt(nameList.size()));
        }

//        System.out.println(surname+name);
        return surname + name;
    }


    public static int getAge(int min, int max) {

        // (max - min) + 1
        return random.nextInt((max - min) + 1) + min;
    }

    public static int getAccumulate(int min, int max) {

        // (max - min) + 1
        return random.nextInt((max - min) + 1) + min;
    }

    public static int getUserClazz(int min, int max) {

        // (max - min) + 1
        return random.nextInt((max - min) + 1) + min;
    }

}
